//
//  KKCheckBox.m
//  Butler
//
//  Created by leo on 16/6/22.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKCheckBox.h"

@implementation KKCheckBox

- (instancetype)initWithTextField:(UITextField*)textField {
    
    self = [super initWithFrame: CGRectMake(0, 0, CGRectGetWidth(textField.bounds), 120)];
    if (self) {
        
        _checkTableView = [[UITableView alloc] initWithFrame: [self bounds]];
        _checkTableView.rowHeight = 30;
        _checkTableView.dataSource = self;
        _checkTableView.delegate = self;
        [self addSubview: _checkTableView];
        _checkTextField = textField;
        _checkTextField.delegate = self;
    }
    return self;
}

#pragma mark - UITextField methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.center = CGPointMake(CGRectGetMinX(_checkTextField.frame) + CGRectGetWidth(_checkTextField.bounds)/2, CGRectGetMaxY(_checkTextField.frame) + CGRectGetHeight(self.bounds)/2);
    [_checkTextField.superview addSubview: self];
    return NO;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [self removeFromSuperview];
    return NO;
}

#pragma mark - UITableView methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [UITableViewCell kk_cellWithTableView: tableView];
    cell.textLabel.text = [NSString stringWithFormat: @"%@", @(indexPath.row + 1)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _checkTextField.text = [NSString stringWithFormat: @"%@", @(indexPath.row + 1)];
}

@end
